// NitroFileFrame.cs:
// Nitro (Nintendo DS) file frame.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License.
// See LICENSE for more information.

using System;
using System.IO;

namespace SharpNitroLib.NARC
{
	/// <summary>
	/// Nitro (Nintendo DS) file frame.
	/// </summary>
    public class NitroFileFrame
    {
    	/// <summary>
    	/// Magic bytes of frame (identifier).
    	/// </summary>
    	public UInt32 Magic  { get; set; }
    	/// <summary>
    	/// Size of raw frame data.
    	/// </summary>
        public UInt32 DataSize
        {
            get
            {
                return _dataSize;
            }
        }
        /// <summary>
    	/// Size of complete frame including header.
    	/// </summary>
        public UInt32 FrameSize
        {
            get
            {
                return DataSize+8;
            }
        }

        /// <summary>
        /// Raw frame data bytes.
        /// </summary>
        private byte[] _frameData;
        
        /// <summary>
        /// Raw frame data bytes.
        /// </summary>
        public byte[] FrameData
        {
            get { return _frameData; }
            set
            {
                _frameData = value;
                CalculateDataSize();
            }
        }

        /// <summary>
    	/// Size of raw frame data.
    	/// </summary>
        private UInt32 _dataSize;

        /// <summary>
        /// Public ctor.
        /// </summary>
        public NitroFileFrame()
        {
            CalculateDataSize();
        }

        /// <summary>
        /// Calculate size of raw data.
        /// </summary>
        private void CalculateDataSize()
        {
            var oldFrameSize = FrameSize;
            if (null == FrameData)
                _dataSize = 0;
            else
                _dataSize = (UInt32)FrameData.Length;

            if (oldFrameSize != FrameSize)
                InvokeFrameSizeChanged();
        }

        /// <summary>
        /// Event handler for frame size changed event.
        /// </summary>
        public event Action<NitroFileFrame, UInt32> FrameSizeChanged;

        /// <summary>
        /// Invoke frame size changed event.
        /// </summary>
        public void InvokeFrameSizeChanged()
        {
            if (null == FrameSizeChanged)
                return;
            FrameSizeChanged.Invoke(this,FrameSize);
        }
    }


}